package ke05;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Random;

public class aufgabe1 {

	public static void main(String[] args) {

		File myDirectory = new File("aufgabe1");
		myDirectory.mkdir();
		for (int i = 1; i <= 200; i++) {
			new File(myDirectory, Integer.toString(i)).mkdir();
		}

		Random r = new Random();
		int i = 0;
		while (i < 5) {
			int ordner = r.nextInt(200) + 1;
			try {
				new File(new File(myDirectory, Integer.toString(ordner)),
						"file" + i + ".bmp").createNewFile();
				// Alternative:
				// new File(myDirectory.getAbsolutePath() + File.separator
				// + ordner, "file" + i + ".bmp").createNewFile();

			} catch (IOException e) {
				e.printStackTrace();
			}
			i++;
		}
		
		String sep = File.separator;
		for(int j = 1; j <= 200; j++){
			File dir = new File("aufgabe1"+sep+j);
			File[] subfiles = dir.listFiles(new FilenameFilter(){

				@Override
				public boolean accept(File dir, String name) {
					return new File(dir, name).isFile() 
							&& name.toLowerCase().endsWith(".bmp");
				}
			});
			
			for(File f: subfiles){
				System.out.println("Ordner:" + f.getParent());
				System.out.println("Umbennenung erfolgreich? "
						+ f.renameTo(new File(f.getAbsolutePath().replaceAll("bmp", "txt"))));
			}
		}
	}
}
